<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!-- jQplot CSS -->

<link rel="stylesheet" media="screen" href="css/Tree.css" />
<!-- jQplot CSS END -->

<!-- DATATABLES CSS -->
<link rel="stylesheet" media="screen" href="lib/datatables/css/vpad.css" />
<script type="text/javascript" src="${ctx }/datepicker/WdatePicker.js"></script>
<script type="text/javascript" src="${ctx}/js/cookie.js"></script>
<script type="text/javascript" src="${ctx}/js/global_util.js"></script>
<!-- DATATABLES CSS END -->
<s:if test="#request.plan != null && #request.plan.id > 0">
	<h1 class="page-title">外训申请 - 修改</h1>
</s:if>
<s:else>
	<h1 class="page-title">外训申请 - 添加</h1>
</s:else>
<div class="container_12 clearfix leading">
	<div class="side grid_12 leading">
		<form id="post_form" name="post_form" class="form has-validation">
			<s:if test="#request.plan != null && #request.plan.id > 0">
				<input id="planid" type="hidden" name="plan.id" value="${plan.id}" />
			</s:if>
			<input id="orgid" type="hidden" name="plan.organization.id"
				value="${plan.organization.id}" />
			<div class="clear"></div>
			<div class="clearfix">
				<label for="form-email" class="form-label">培训课程 <em>*</em></label>
				<div class="form-input">
					<input type="text" id="title" name="plan.title" required="required"
						placeholder="请输入培训课程" value="${plan.title}" />
				</div>
			</div>


			<div class="clear"></div>
			<div class="clearfix">
				<label for="form-email" class="form-label">开始日期(时间)<em>*</em></label>
				<div class="form-input">
					<input type="text" id="planStartDate" name="planStartDate"
						required="required" placeholder="请输入开始日期及时间"
						onfocus="WdatePicker({maxDate: '#F{$dp.$D(\'planEndDate\')}',dateFmt:'yyyy-MM-dd HH:mm'})"
						value='<s:date name="#request.plan.planStartDate" format="yyyy-MM-dd HH:mm"/>' />
				</div>
			</div>

			<div class="clear"></div>
			<div class="clearfix">
				<label for="form-email" class="form-label">结束日期(时间) <em>*</em></label>
				<div class="form-input">
					<input type="text" id="planEndDate" name="planEndDate"
						required="required" placeholder="请输入日期及时间"
						onfocus="WdatePicker({minDate: '#F{$dp.$D(\'planStartDate\')}',dateFmt:'yyyy-MM-dd HH:mm'})"
						value='<s:date name="#request.plan.planEndDate" format="yyyy-MM-dd HH:mm"/>' />
				</div>
			</div>

			<div class="clear"></div>
			<div class="clearfix">
				<input type="hidden" name="duty_outing_target" id="duty_outing_target" />
				<label for="form-email" class="form-label">申请对象 <em>*</em></label>
				<div class="form-input">
					<span id="outing_employee_has_sel">
						<s:iterator var="target" value="#request.employeeTargets">
							<span>${target.employeeinfoWork.baseId.name}</span>&nbsp;
						</s:iterator>
					</span>
					<a class="button"
						href="javascript:void(0)" onclick="choiceEmployment('${organizationId}','${plan.id > 0 ? plan.id : -1}')"
						style="margin-top: 10px; margin-left: 10px"><span
						style="background: url(images/img/cursor.png)"
						class="icon icon-ok">&nbsp;</span>选择</a>
				</div>
			</div>

			<div class="clear"></div>
			<div class="clearfix">

				<label for="form-email" class="form-label">培训类型 <em>*</em></label>

				<div class="form-input">
					<select id="category" size="1" name="plan.category">
						<s:iterator var="category" status="sta"
							value="#request.categories">
							<option
								<s:if test="#request.plan.id > 0 && #category.title.equals(#request.plan.category)">selected="selected"</s:if>
								value="${category.title}">${category.title}</option>
						</s:iterator>
					</select>
				</div>
			</div>

			<div class="clearfix">
				<label for="form-name" class="form-label">地点 <em>*</em></label>
				<div class="form-input">
					<input type="text" id="classroom" name="plan.classroom"
						required="required" placeholder="请输入地点" value="${plan.classroom}" />
				</div>
			</div>

			<div class="clearfix">

				<label for="form-name" class="form-label">讲师 <em>*</em></label>

				<div class="form-input">
					<input type="text" id="lecturer" name="plan.outingLecturer"
						required="required" placeholder="请输入讲师名字"
						value="${plan.outingLecturer}" />
				</div>

			</div>

			<div class="clear"></div>
			<div class="clearfix">

				<label for="form-email" class="form-label">费用<em>*</em></label>

				<div class="form-input">
					<select size="1" name="plan.outingCharge">
						<option
							<s:if test="#request.plan.outingCharge == null || #request.plan.outingCharge.equals('免费')">selected="selected"</s:if>
							value="免费">免费</option>
						<option
							<s:if test="#request.plan.outingCharge != null && #request.plan.outingCharge.equals('公司支付')">selected="selected"</s:if>
							value="公司支付">公司支付</option>
						<option
							<s:if test="#request.plan.outingCharge != null && #request.plan.outingCharge.equals('其他')">selected="selected"</s:if>
							value="其他">其他</option>
					</select>
				</div>

			</div>

			<div class="clear"></div>
			<div class="clearfix">

				<label for="form-email" class="form-label">预计费用 </label>

				<div class="form-input">
					<input type="text" id="expect_charge"
						name="plan.outingExpectCharge" required="required"
						value="${plan.outingExpectCharge}" />
				</div>

			</div>

			<div class="clear"></div>
			<div class="clearfix">

				<label for="form-email" class="form-label">申请理由 </label>

				<div class="form-input">
					<textarea name="plan.outingApplyReason" required="required"
						id="apply_reason" placeholder="申请理由">${plan.outingApplyReason}</textarea>
				</div>

			</div>

			<div class="clear"></div>
			<div class="clearfix">
				<label for="form-email" class="form-label">受训反馈需求</label>
				<div class="form-input">
					<div class="radiogroup">
						<label> <input type="checkbox" name="plan.outingFeedback"
							value="1"
							<s:if test="#request.plan.outingFeedback != null && #request.plan.outingFeedback.indexOf('1') > -1">checked="checked"</s:if> />
							外训意见回馈单
						</label> <label> <input type="checkbox" name="plan.outingFeedback"
							value="0"
							<s:if test="#request.plan.outingFeedback != null && #request.plan.outingFeedback.indexOf('0') > -1">checked="checked"</s:if> />
							心得报告
						</label> <input type="checkbox" name="plan.outingFeedback" value="2"
							<s:if test="#request.plan.outingFeedback != null && #request.plan.outingFeedback.indexOf('2') > -1">checked="checked"</s:if> />
						发票或收据复印件 <input type="checkbox" name="plan.outingFeedback"
							value="3" 
							<s:if test="#request.plan.outingFeedback != null && #request.plan.outingFeedback.indexOf('3') > -1">checked="checked"</s:if> /> 结业证书 <input type="checkbox"  name="plan.outingFeedback" value="4"
							<s:if test="#request.plan.outingFeedback != null && #request.plan.outingFeedback.indexOf('4') > -1">checked="checked"</s:if> />
						出差报告单
					</div>
				</div>
			</div>
			<div class="form-action clearfix">
				<button class="button" type="button" onclick="checkForm()">
					<s:if test="#request.plan.id > 0">修改</s:if>
					<s:else>添加</s:else>
				</button>
				<button class="button" type="reset">重置</button>
				<a class="button" href="javascript:void(0)"
					onclick="javascript:history.back()"><span
					style="background: url(${ctx}/images/img/arrow_undo.png)"
					class="icon">&nbsp;</span>返回</a>

			</div>
		</form>
	</div>
</div>
<form id="session_form" name="session_form" style="display: none;"></form>
<div class="clear"></div>

<script type="text/javascript">

	$(document).ready(function(){
		//$("#duty_outing_target").val(getCookie("duty_outing_target") != null ? getCookie("duty_outing_target") : "");
		//updateSelectBody();
	});

	function updateSelectBody() {
		$("#outing_employee_has_sel").html("");
		if (getCookie("duty_outing_target") != null) {
			var totalTargetArray = getCookie("duty_outing_target").match(
					/(\{(\s|\S)+?\})+?/g);
			if (totalTargetArray == null) {
				totalTargetArray = new Array();
				//$("#outing_employee_has_sel").html("暂无选择任何对象");
			}
			for ( var i = 0; i < totalTargetArray.length; i++) {
				var obj = eval("(" + totalTargetArray[i] + ")");
				$("#outing_employee_has_sel")
						.append(
								" <span>"
										+ unescape(obj.workName)
										+ "</span> ");
			}
		} else {
			//$("#outing_employee_has_sel").html("暂无选择任何对象");
		}
	}
	
	function checkForm() {
		if ($("#title").val() == null || $("#title").val() == "") {
			alert("请输入名称");
			$("#title").focus();
			return;
		}

		if ($("#planStartDate").val() == null
				|| $("#planStartDate").val() == "") {
			alert("请输入开始日期(时间)");
			return;
		}

		if ($("#planEndDate").val() == null || $("#planEndDate").val() == "") {
			alert("请输入结束日期(时间)");
			return;
		}

		if ($("#classroom").val() == null || $("#classroom").val() == "") {
			alert("请设置培训室资源");
			return;
		}
		if ($("#lecturer").val() == null || $("#lecturer").val() == "") {
			alert("请设置讲师");
			return;
		}
		
		if ($("#expect_charge").val() == null || $("#expect_charge").val() == "") {
			alert("请输入预计费用");
			return;
		}
		
		if (isNaN($("#expect_charge").val())) {
			alert("请输入有效的数字费用");
			return;
		}
		
		
		/* $("#duty_outing_target").val(
				getCookie("duty_outing_target") != null ? getCookie("duty_outing_target") : "");
		if ($("#duty_outing_target").val() == "") {
			alert("请选择培训对象");
			return;
		}
		delCookie("duty_outing_target"); */
		$.ajax("${ctx}/ajax/check_employeetarget.xhtml",{
			type : "POST",
			//data : $("#post_form").serialize(),
			async:false,
			dataType : "html",
			cache : false,
			success : function(a) {
				if(a != null && a == "0"){
					alert("请选择培训员工对象");
					return;
				}else{
					var url = "${ctx}/train/post_outing_train_plan.xhtml";
					$
							.ajax(
									url,
									{
										type : "POST",
										data : $("#post_form").serialize(),
										dataType : "html",
										cache : false,
										success : function(a) {
											alert("提交成功");
											window.location.href = "${ctx}/index.xhtml#train/outing_train_main.xhtml?organizationId=${plan.organization.id}";
										},
										error : function(XMLHttpRequest, textStatus,
												errorThrown) {
											alert('提交申请发生异常');
										},
										complete : function() {
										}
									});

				}
			},
			error : function(XMLHttpRequest, textStatus,
					errorThrown) {
				alert('检测培训员工对象申请发生异常');
				return;				
			},
			complete : function() {
			}
		});
		
	}

	
	//选择协助部门时，先将填写的表单保存到session
    function choiceEmployment(organizationId,planId){
		//organizationId=${organizationId}&planId
		var orgid = $("#orgid").val();
        if (!checkNull(orgid)) {
            $("#session_form").append("<input type='' name='plan.organization.id' value='"+orgid+"'>");
        }
        
        var planid = $("#planid").val();
        if (!checkNull(planid)) {
            $("#session_form").append("<input type='' name='plan.id' value='"+planid+"'>");
        }
        
        var title = $("#title").val();
        if (!checkNull(title)) {
            $("#session_form").append("<input type='' name='plan.title' value='"+title+"'>");
        }
        var planStartDate = $("#planStartDate").val();
        if (!checkNull(planStartDate)) {
            $("#session_form").append("<input type='' name='planStartDate' value='"+planStartDate+"'>")
        }
        var planEndDate = $("#planEndDate").val();
        if (!checkNull(planEndDate)) {
            $("#session_form").append("<input type='' name='planEndDate' value='"+planEndDate+"'>")
        }
        var category = $("#category").val();
        if (!checkNull(category)) {
            $("#session_form").append("<input type='' name='plan.category' value='"+category+"'>")
        }
        var classroom = $("#classroom").val();
        if (!checkNull(classroom)) {
            $("#session_form").append("<input type='' name='plan.classroom' value='"+classroom+"'>")
        }
        var lecturer = $("#lecturer").val();
        if (!checkNull(lecturer)) {
            $("#session_form").append("<input type='' name='plan.outingLecturer' value='"+lecturer+"'>")
        }
        var outingCharge = $("#outingCharge").val();
        if (!checkNull(outingCharge)) {
            $("#session_form").append("<input type='' name='plan.outingCharge' value='"+outingCharge+"'>")
        }
        var expect_charge = $("#expect_charge").val();
        if (!checkNull("expect_charge")) {
            $("#session_form").append("<input type='' name='plan.outingExpectCharge' value='"+ expect_charge + "'>")
        }
        var apply_reason = $("#apply_reason").val();
        if (!checkNull(apply_reason)) {
            $("#session_form").append("<input type='' name='plan.outingApplyReason' value='"+apply_reason+"'>")
        }
        var chk_value =[];    
        $('input[name="plan.outingFeedback"]:checked').each(function(){    
         	chk_value.push($(this).val());    
        });
        if (chk_value.length > 0 ) {
            $("#session_form").append("<input type='' name='plan.outingFeedback' value='"+chk_value+"'>")
        }
        
        var data = $("#session_form").serialize();
        if(data != null && data.length > 0){
            var url = "${ctx}/ajax/train-outing-apply-form-save.xhtml";
            $.ajax(url, {
                type:"POST",
                data:data,
                dataType:"html",
                cache:false,
                success:function (a) {
                	window.location.href = "#train/outing_train_target_sel_main.xhtml?organizationId=" + organizationId + "&planId=" + planId;
                },
                complete:function () {
                	window.location.href = "#train/outing_train_target_sel_main.xhtml?organizationId=" + organizationId + "&planId=" + planId;
                }
            });
        }else{
            window.location.href = "#train/outing_train_target_sel_main.xhtml?organizationId=" + organizationId + "&planId=" + planId;
        }
    }
	
</script>